﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

public class MovieManager
{
    private debioscoopEntities db;

    public MovieManager(debioscoopEntities db)
	{
        this.db = db;
	}

    public void addMovie(film tempFilm)
    {
        db.films.Add(tempFilm);
        db.SaveChanges();
    }

    public film getMovie(int movieID)
    {
        foreach (var tempMovie in db.films)
        {
            if (tempMovie.film_id == movieID)
            {
                return tempMovie;
            }
        }

        return null;
    }

    public List<film> getMovies()
    {
        return db.films.ToList();
    }

    public List<film> getMoviesByDate(DateTime date, bool timeNow = true)
    {
        List<planning> planningen = db.plannings.Where(p => p.datum == date.Date).ToList();
        List<film> movies = new List<film>();
        foreach (var planning in planningen)
        {
            if (!movies.Contains(planning.film))
            {
                if ((timeNow == true && planning.tijd >= DateTime.Now.TimeOfDay) || timeNow == false)
                {
                    movies.Add(planning.film);
                }
            }
        }
        return movies;
    }

    public String getGenreInString(film tempFilm, string delimiter, string tempLanguage, bool space = false)
    {
        string movieGenres = "";
        int i = 0;
        foreach (genre tempGenre in tempFilm.genres)
        {
            movieGenres += (i > 0 ? (space ? " " : "") + delimiter + " " : "") + (tempLanguage.Equals("dutch") ? tempGenre.genre1 : tempGenre.genre_english);
            i++;
        }
        return movieGenres;
    }

    public String getMovieTypes(film tempFilm)
    {
        string movieTypes = "OV";
        foreach (planning tempPlanning in tempFilm.plannings)
        {
            if (tempPlanning.dried == true)
            {
                movieTypes = "OV, 3D";
                return movieTypes;
            }
        }
        return movieTypes;
    }

    public String getSummary(film tempFilm, String tempLanguage, int substring = 0)
    {
        String returnSummary = "";
        switch (tempLanguage)
        {
            case "dutch":
                returnSummary = tempFilm.omschrijving;
            break;
            case "english":
                returnSummary = tempFilm.omschrijving_en;
            break;
        }

        if (substring == 0)
        {
            substring = returnSummary.Length;
        }
        else if (substring > returnSummary.Length)
        {
            substring = returnSummary.Length;
        }
        returnSummary = returnSummary.Substring(0, substring);
        return returnSummary;
    }

    public List<film> getMoviesComingSoon()
    {
        return db.films.Where(m => m.coming_soon == true).ToList();
    }

    public bool reservePosible(planning tempPlanning)
    {
        if(tempPlanning.film.reserveerbaar && tempPlanning.tijd > DateTime.Now.TimeOfDay.Add(new TimeSpan(0, +30, 0)))
        {
            double percent = (double)tempPlanning.film.reserverings_percentage;
            double tickets = tempPlanning.kaartjes.Count;
            double seatsAvailable = tempPlanning.zaal.zitting;

            double percentTaken = (tickets / seatsAvailable) * 100;

            if (percent > percentTaken)
            {
                return true;
            }
        }
        return false;
    }
}